package com.heima.test1;


import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Random;

class B implements Runnable {
    ArrayList<String> list01 = new ArrayList<>();
    Random r = new Random();

    public B() {
        Collections.addAll(list01, "电视机", "电冰箱", "电脑", "游戏机", "洗衣机", "空调", "手机", "平板电脑", "电动车", "电饭煲");
    }

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            synchronized (this) {
                if (list01.size() == 0) {
                    break;
                }
                int index = r.nextInt(list01.size());
                String jiangpin = list01.remove(index);
                System.out.println(Thread.currentThread().getName() + "抽到了" + jiangpin);
            }
        }
    }
}


public class Demo03 {
    public static void main(String[] args) {
        B b = new B();
        new Thread(b, "张三").start();
        new Thread(b, "李四").start();
        new Thread(b, "王五").start();
    }
}
